<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>axios.create()</title>
  </head>
  <body>
    <div>
      <button onclick="testGet()">GET请求</button>
      <button onclick="testPost()">POST请求</button>
      <button onclick="testPut()">PUT请求</button>
      <button onclick="testDelete()">DELETE请求</button>
    </div>

    <script src="https://cdn.bootcss.com/axios/0.19.0/axios.js"></script>

    <script>
      axios.defaults.baseURL = "http://localhost:3000";

      function testGet() {
        axios({
          url: "/posts",
          method: "get",
          params: {
            _start: 1,
            _end: 3,
            _limit: 2,
          },
        }).then((response) => {
          console.log(response.data);
        });

        /* axios
          .get("http://localhost:3000/posts", {
            params: {
              _start: 1,
              _end: 3,
              _limit: 2,
            },
          })
          .then((response) => {
            console.log(response.data);
          }); */
      }

      function testPost() {
        axios({
          url: "/posts",
          method: "post",
          data: {
            id: 6,
            title: "json-server4",
            author: "typicode4",
          },
        }).then((response) => console.log(response.data));

        /* axios
          .post("http://localhost:3000/posts", {
            id: 4,
            title: "json-server4",
            author: "typicode4",
          })
          .then((response) => console.log(response.data)); */
      }

      function testPut() {
        axios({
          url: "/posts/4",
          method: "put",
          data: {
            title: "json-server...",
            author: "typicode...",
          },
        }).then((response) => console.log(response.data));

        /* axios
          .put("http://localhost:3000/posts/4", {
            title: "json-server...",
            author: "typicode...",
          })
          .then((response) => console.log(response.data)); */
      }

      function testDelete() {
        axios({
          url: "/posts/4",
          method: "delete",
        }).then((response) => console.log(response.data));

        /* axios
          .delete("http://localhost:3000/posts/3")
          .then((response) => console.log(response.data)); */
      }
    </script>
  </body>
</html>
